import {getAccountRecord,delAccountRecord} from '../../api/member.js';
import Util from '../../utils/util.js';

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    tabNavs: [
      {title: "全部", type: 0},
      {title: "收入", type: 2},
      {title: "支出", type: 1},
    ],

    loading: false, // 是否加载中
    loadend: false, // 是否加载完毕
    loadTitle: '加载更多内容', // 提示语
    lists: [],
    page: 1,
    et_show: true,

    current: 0,

    startX: 0,
    startY: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      bottom: app.globalData.footerPadding,
    })

    this.setData({
      loadend: false,
      loading: false,
      page: 1,
      lists: []
    });
    this.getLists();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  jumpDetail(e){
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: 'consume_detail?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  getLists() {
    const that = this

    if (that.data.loadend || that.data.loading) {
      return;
    }

    let filter = that.data.filter || {}
    filter['page_num'] = that.data.page;
    if (this.data.current > 0)
      filter['type'] = this.data.tabNavs[this.data.current].type;

    that.setData({loading: true, loadTitle: '正在加载...'});
    let lists = that.data.lists || []
    getAccountRecord(filter).then(res => {
      const list = res.data.items || []
      const loadend = res.data.last_page <= that.data.page;

      lists = app.SplitArray(list, lists);

      that.setData({
        lists: lists,
        loadend: loadend,
        loading: false,
        loadTitle: loadend ? '已全部加载' : '正在加载...',
        total: res.data.total,
        page: that.data.page + 1,
        et_show: lists.length > 0
      });

      wx.stopPullDownRefresh()
    }).catch(errmsg => {
      wx.stopPullDownRefresh()
      app.Tips({title: errmsg})
    })

  },

  del(e) {
    const that = this,
      key = e.currentTarget.dataset.index

    let lists = that.data.lists || []
    wx.showModal({
      title: '消息删除后将不可恢复，确认删除吗？',
      content: '',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: "#EF6243",
      success: (res) => {
        if (res.confirm) {
          delAccountRecord(lists[key].id).then(res => {
            lists.splice(key, 1)
            that.setData({
              lists: lists
            })
          }).catch(errmsg => {
            app.Tips({title: errmsg})
          })
        }
      }
    })
  },

  /**
   * 手指触摸动作开始，记录起始点
   */
  touchstart(e) {
    const pageX = e.changedTouches[0].pageX
    const pageY = e.changedTouches[0].pageY;
    // 遍历moveList，把每一项的isView设置为true
    this.data.lists.forEach((item, index) => {
      if (item.isView) {
        item.isView = false
      }
    })
    this.setData({
      startX: pageX,
      startY: pageY,
      lists: this.data.lists
    })
  },

  /**
   * 滑动事件处理
   */
  touchmove(e) {
    const pageX = e.changedTouches[0].pageX;//结束时X坐标
    const pageY = e.changedTouches[0].pageY;//结束时Y坐标
    const startX = this.data.startX;//开始时X坐标
    const startY = this.data.startY;//开始时Y坐标
    const that = this
    // 获取华东角度
    const angle = this.angle({X: startX, Y: startY}, {X: pageX, Y: pageY})
    // 当前索引
    const index = e.currentTarget.dataset.index
    this.data.lists.forEach((item, i) => {
      // 1.如果滑动角度大于30度，直接返回
      //2.如果开始位置小于结束位置，说明时向右滑动

      if (Math.abs(angle > 30)) {
        return
      }
      if (i == index) {
        if (that.data.startX < pageX) {//右滑
          item.isView = false
        } else {
          //左滑
          item.isView = true
        }
      }
    })

    this.setData({
      lists: this.data.lists
    })
  },

  /**
   * 计算角度的函数
   */
  angle: function (start, end) {
    var _X = start.X - end.X;
    var _Y = start.Y - end.Y;
    //   返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI)
  },
  changeTabbar(e) {  //tab 切换
    const key = e.currentTarget.dataset.key
    let tabbars = this.data.tabNavs;

    for (const tab in tabbars) {
      if (key == tab) {
        tabbars[tab].class = 'active';
      } else {
        tabbars[tab].class = '';
      }
    }

    this.setData({
      loadend: false,
      page: 1,
      word: e.detail,
      lists: [],
      tabNavs: tabbars,
      current: key
    })

    this.getLists()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      loadend: false,
      loading: false,
      page: 1,
      word: "",
      lists: []
    });
    this.getLists();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getLists();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  }
})